<script setup >
import Navbar from "./components/Navbar.vue"; 
import Carousel from "./components/Carousel.vue"; 
import CateScroll from "./components/CateScroll.vue"; 
import HotBrand from "./components/HotBrand.vue"; 
import Guess from "./components/Guess.vue"; 
import * as home from '@/hooks/home'
import {  getLikesAPI } from "@/api/home";
import { ref, onMounted } from 'vue';

const  banners =ref([])
const  Category =ref([])
const  hots =ref([])
const  newGoods =ref([])
const  likes = ref([])
const params = {
  page: 1,
  pageSize:10
}
onMounted(() => {
  home.loadCategory(Category)
  home.loadBanners(banners)
  home.loadHots(hots)
  home.loadNewGoods(newGoods)
  loadLikes()
})

// 自定义自己的分页数据
let pages = 1

// 封装猜你喜欢请求函数调用api
const loadLikes = async () => {
  const { result } = await getLikesAPI(params) 
  console.log("111",result);
  likes.value = [...likes.value, ...result.items]
  pages=result.pages
}

const onScrolltolower = () => {
  // 判断下一页是否还有数据
  if (params.page >= pages) {
    uni.showToast({ title: "没有更多数据啦", icon: "none" })
  return
  }
  params.page++
  loadLikes()
}

// 下拉刷新
const refresherTriggered = ref(false)
// 下拉刷新事件
const onRefresherrefres = async ()=> {
  refresherTriggered.value = true;
  // 重置页码
  params.page = 1
  // 重置数据
  likes.value = []
  await Promise.all(
    [home.loadBanners(banners),
    home.loadCategory(Category),
    home.loadHots(hots),
    home.loadNewGoods(newGoods),
    loadLikes()])
     // 等待所有的请求结束 才关闭
  refresherTriggered.value = false;
}

</script>

<template>
  <view class="content">
    <navbar />
    <scroll-view 
    class="main" 
    scroll-y
    refresher-enabled
    @scrolltolower="onScrolltolower"
    :refresher-triggered="refresherTriggered"
    @refresherrefresh="onRefresherrefres"
    >
      <Carousel :banners="banners" height="280rpx" />
      <CateScroll :Category="Category" />
      <!--  热门推荐 -->
      <view class="panel recommend">
        <view class="item" v-for="item in hots" :key="item.id">
          <view class="title">
            {{ item.title }}<text>{{ item.alt }}</text>
          </view>
          <navigator hover-class="none" :url="`/pages/recommend/index?type=${item.type}`" class="cards">
            <image mode="aspectFit" v-for="img in item.pictures" :key="img" :src="img"></image>
          </navigator>
        </view>
      </view>
      <!-- 新鲜好物 -->
      <view class="panel fresh">
        <view class="title">
          新鲜好物
          <navigator
          hover-class="none"
          class="more"
          url="/pages/recommend/index?type=5"
          >更多</navigator
          >
        </view>
        <view class="cards">
          <navigator
            hover-class="none"
            :url="`/pages/goods/index?id=${item.id}`"
            v-for="item in newGoods"
            :key="item.id"
          >
            <image mode="aspectFit" :src="item.picture"></image>
            <view class="name">{{ item.name }}</view>
            <view class="price">
              <text class="small">¥</text>{{ item.price }}
            </view>
          </navigator>
        </view>
      </view>
      <HotBrand/>
      <Guess :likes="likes"/>
</scroll-view>
  </view>
  
</template>


<style lang="scss" scoped>
.content {
  height: 100vh;
  display: flex;
  flex-direction: column;
}


.main {
  flex: 1;
  overflow: hidden;
  background-color: #f7f7f8;
}


/* 公共面板 */
.panel {
  margin: 20rpx 20rpx 0;
  border-radius: 10rpx;
  background-color: #fff;


  .title {
    display: flex;
    align-items: center;
    padding: 24rpx 24rpx 0;
    font-size: 32rpx;
    color: #262626;
    position: relative;


    text {
      font-size: 24rpx;
      color: #7f7f7f;
      margin-left: 18rpx;
    }
  }


  .more {
    position: absolute;
    right: 24rpx;
    font-size: 26rpx;
    color: #7f7f7f;
  }


  .cards {
    display: flex;
  }


  .name {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
}


/* 推荐专区 */
.recommend {
  display: flex;
  flex-wrap: wrap;


  .item {
    display: flex;
    flex-direction: column;
    width: 50%;
    height: 254rpx;
    border-right: 1rpx solid #eee;
    border-top: 1rpx solid #eee;


    &:nth-child(2n) {
      border-right: 0 none;
    }


    &:nth-child(-n + 2) {
      border-top: 0 none;
    }
  }


  .cards {
    flex: 1;
    padding: 15rpx 20rpx;
    justify-content: space-between;


    image {
      width: 48%;
    }
  }
}


/* 新鲜好物 */
.fresh {
  min-height: 320rpx;


  .price {
    line-height: 1;
    text-align: left;
    font-size: 26rpx;
    color: #cf4444;
  }


  .small {
    font-size: 80%;
  }
}


.fresh .cards navigator,
.brands .cards navigator {
  width: 25%;
  padding: 15rpx 20rpx 20rpx;
  line-height: 1;
  text-align: center;
}


.fresh image,
.brands image {
  width: 126rpx;
  height: 126rpx;
}


.fresh .name,
.brands .name {
  margin: 20rpx 0 16rpx;
  font-size: 24rpx;
  color: #262626;
}


.brands {
  .price {
    font-size: 24rpx;
    color: #999;
  }
}
</style>
